package com.hxzy.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.annon.Log;
import com.hxzy.common.annon.RateLimiter;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.vo.R;
import com.hxzy.common.web.controller.BaseController;
import com.hxzy.dto.SysDictTypeDTO;
import com.hxzy.entity.SysDictType;
import com.hxzy.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(tags = "字典管理")
@RequestMapping("/api/dicttype")
@RestController
public class SysDictTypeController extends BaseController {
    @Autowired
    private SysDictTypeService sysDictTypeService;

   // @Autowired
    //private DictDataCache dictDataCache;


    @ApiOperation(value = "分页获取字典列表")
   //@PreAuthorize(value = "@ss.hasAuthority('system:dict:query')")
    @GetMapping(value = "/data")
    public R list(SysDictTypeDTO sysDictTypeDTO) {
        IPage<SysDictType> page = this.sysDictTypeService.selectDictTypeList(sysDictTypeDTO);
        return super.pageToPageVO(page);
    }

   // @PreAuthorize(value = "@ss.hasAuthority('system:dict:query')")
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/{dictId}")
    public R selectTypeId(@PathVariable Long dictId) {
        SysDictType selectid = this.sysDictTypeService.getById(dictId);
        if (Objects.isNull(selectid)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        selectid.setUpdateBy(null);
        selectid.setUpdateTime(null);
        selectid.setCreateBy(null);
        selectid.setCreateTime(null);
        return R.okHasData(selectid);
    }

    //@PreAuthorize(value = "@ss.hasAuthority('system:dict:query')")
    @ApiOperation(value = "根据类型查询列表框")
    @GetMapping("/optionselect")
    public R optionselect(){
        LambdaQueryWrapper<SysDictType> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictType::getStatus, "0");
        queryWrapper.select(SysDictType::getDictId,SysDictType::getDictName,SysDictType::getDictType);
        List<SysDictType> list = this.sysDictTypeService.list(queryWrapper);
        return R.okHasData(list);
    }

   // @Log(title = "字典管理",businessType = BusinessType.INSERT)
   // @PreAuthorize(value = "@ss.hasAuthority('system:dict:add')")
    @ApiOperation(value = "新增字典类型")
    @PostMapping
    public R add(@RequestBody @Validated  SysDictType sysDictType) throws IllegalAccessException {
        //判断值是否重复
//        super.adminValidateExistsValue(sysDictType,"dict_name",sysDictType.getDictName());
//        super.adminValidateExistsValue(sysDictType,"dict_type",sysDictType.getDictType());

        boolean result = this.sysDictTypeService.addDict(sysDictType);
        return  super.toR(result);
    }

    @Log(title = "字典管理",businessType = BusinessType.UPDATE)
   // @PreAuthorize(value = "@ss.hasAuthority('system:dict:edit')")
    @ApiOperation(value = "修改字典类型")
    @PutMapping
    public R updateType(@RequestBody @Validated SysDictType sysDictType) throws IllegalAccessException {
        SysDictType dbDict = this.sysDictTypeService.getById(sysDictType.getDictId());
        if(dbDict==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        //判断，dictType类型的值不允许修改的
        if(!dbDict.getDictType().equals(sysDictType.getDictType())){
            return R.build(AckCode.Modi_DICTTYPE_NOT_ALLOWED);
        }

        //判断值是否重复
//        super.adminValidateExistsValue(sysDictType,"dict_name",sysDictType.getDictName());
//        super.adminValidateExistsValue(sysDictType,"dict_type",sysDictType.getDictType());


       boolean result = this.sysDictTypeService.updateDict(sysDictType,dbDict);

        return super.toR(result);
    }

   // @Log(title = "字典管理",businessType = BusinessType.DELETE)
   // @PreAuthorize(value = "@ss.hasAuthority('system:dict:remove')")
    @ApiOperation(value = "删除字典")
    @DeleteMapping("/{dictIds}")
    public R delType(@PathVariable Long[]  dictIds){
        boolean result = this.sysDictTypeService.deleteDictTypeByIds(dictIds);
        return  super.toR(result);
    }

    /**
     * 刷新字典缓存
     */
   // @RateLimiter(time = 60,count = 5)
   // @Log(title = "字典类型", businessType = BusinessType.UPDATE)
   // @PreAuthorize(value = "@ss.hasAuthority('system:dict:edit')")
    @GetMapping("/refreshCache")
    public R refreshCache()
    {
//        this.dictDataCache.clearRedisAllData();
//        this.dictDataCache.loadAllDataToRedis();
        return R.ok();
    }



}
